package com.pospi.xstore.ui.cash_ui;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.pospi.xstore.App;
import com.pospi.xstore.R;
import com.pospi.xstore.adapter.TableAdapter;
import com.pospi.xstore.dto.Tablebeen;
import com.pospi.xstore.greendao.TablebeenDao;
import com.pospi.xstore.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by acer on 2018/6/12.
 */

public class TableActivity extends BaseActivity {
    @Bind(R.id.gridView)
    GridView gridView;
    @Bind(R.id.parent_table)
    LinearLayout parent_table;
    @Bind(R.id.valuable_table)
    TextView valuable_table;

    private List<Tablebeen> tablebeens = new ArrayList<>();

    private Tablebeen tablebeen;
    private TablebeenDao dao;
    private TableAdapter tableAdapter;
    private EditText et_table;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_table);
        setTitle("桌台");
        ButterKnife.bind(this);
        dao = App.getInstance().getDaoSession().getTablebeenDao();
        init();

    }
    private int checkTables(){
        int number = 0;
        for (Tablebeen tb : tablebeens) {
            if (tb.getStatus() == 0) {
                number++;
            }
        }
        return number;
    }

    public void init() {
        tablebeens = dao.loadAll();
//        for (int i=1;i<10;i++) {
//            Tablebeen tablebeen = new Tablebeen();
//            tablebeen.setNumber(i + "");
//            tablebeen.setStatus(i%2);
//            tablebeens.add(tablebeen);
//        }
        valuable_table.setText(String.valueOf(checkTables()));

        tableAdapter = new TableAdapter(this, tablebeens);
        gridView.setAdapter(tableAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (tablebeens.get(i).getStatus() != 0) {
                    Toast.makeText(TableActivity.this, "餐桌已选", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    showWindow(tablebeens.get(i));
                }

            }
        });
//        tableDao = new TableDao(this);
//        orderDao = new OrderDao(this);
//        eatingNumber = 1;
    }
    private void showWindow(final Tablebeen tb){
        View contentView = LayoutInflater.from(TableActivity.this).inflate(R.layout.table_pop, null);
        final PopupWindow popupWindow = new PopupWindow(contentView, getWidth()*2/3,getHeight()*3/5);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(parent_table, Gravity.CENTER, 0, 0);
        TextView sure = (TextView) contentView.findViewById(R.id.sure_table);
        TextView name_table = (TextView) contentView.findViewById(R.id.name_table);
        et_table = (EditText) contentView.findViewById(R.id.et_table);
        TextView clear_table = (TextView) contentView.findViewById(R.id.clear_table);
        name_table.setText(tb.getNumber()+"号桌");

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                Intent intent = new Intent(TableActivity.this, PointSelectActivity.class);
                intent.putExtra("table", tb);
                startActivity(intent);
                finish();
            }
        });
        clear_table.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_table.setText("");
                numberAdd = "";
            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                numberAdd = "";
            }
        });

    }
    private String numberAdd = "";

    public void popupWindowNumberClick(View view) {
        TextView tv = (TextView) view;
        if (numberAdd.isEmpty()) {
            if (view.getId() != R.id.dian) {
                numberAdd += tv.getText().toString();
            }
        } else {
            if (numberAdd.equals("0") && view.getId() == R.id.dian) {
                numberAdd = tv.getText().toString();
            } else {
                numberAdd += tv.getText().toString();
            }
        }
        et_table.setText(numberAdd);
    }
    private int  getWidth(){
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        float density = dm.density;
        return dm.widthPixels;
    }
    private  int getHeight(){
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        float density = dm.density;
        return dm.heightPixels;
    }

}
